const { Router } = require("express");
const { SuccessResult, ErrorResult } = require("../src/utils");
const db = require("../src/tools/mysql");

const router = Router();

router.get("/user/:name", (req, res) => {
  
  res.send(SuccessResult("请求成功！"))
})
router.post("/user", (req, res) => {
  // throw new Error("这是一个错误");
  res.send(SuccessResult("POST 请求成功！"))
});

router.put("/user", (req, res) => {
  const user = req.query;
  const sqlStr = 'INSERT INTO sys_user SET ?'
  db.query(sqlStr, user, (err, results) => {
    if(err) return console.log(err.message);
    if(results.affectedRows === 1) {
      res.send(SuccessResult("用户保存成功！"));
    } else {
      res.send(ErrorResult("用户保存失败！"));
    }
  })
});

module.exports = router;